/**
 * 
 */
package com.wubo.sec.web.action;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.wubo.common.util.StringUtils;
import com.wubo.sec.bean.RoleAuthRelate;
import com.wubo.sec.service.IRoleAuthService;

import flexjson.JSONSerializer;

/**
 * @author wubo
 * @CreateDate 2010-8-8
 * @version 1.0.01
 */
@Controller
public class RoleAuthMgtAction extends BaseSecAction {
	@Autowired
	private IRoleAuthService roleAuthService;
	
	private String roleCode;
	
	private List<String> authCodes;
	
	private String authCode;

	public IRoleAuthService getRoleAuthService() {
		return roleAuthService;
	}

	public void setRoleAuthService(IRoleAuthService roleAuthService) {
		this.roleAuthService = roleAuthService;
	}

	public String getRoleCode() {
		return roleCode;
	}

	public void setRoleCode(String roleCode) {
		this.roleCode = roleCode;
	}

	public List<String> getAuthCodes() {
		return authCodes;
	}

	public void setAuthCodes(List<String> authCodes) {
		this.authCodes = authCodes;
	}

	public String getAuthCode() {
		return authCode;
	}

	public void setAuthCode(String authCode) {
		this.authCode = authCode;
	}
	
	public String show(){
		try{
			authCodes = roleAuthService.getAuthCodes(roleCode);
			
			logger.debug("Find role's auths .RoleCode:"+roleCode+", AuthCodes:"+authCodes);
			
			jsonSuccess(new JSONSerializer().serialize(authCodes), true);
		}catch(RuntimeException t){
			jsonError(null);
			throw t;
		}
		return null;
	}
	
	public String relate(){
		logger.debug("relate auths to Role .RoleCode:"+roleCode+", AuthCodes:"+authCodes);

		try{
			String[] temp = null;
			if(StringUtils.hasText(authCode)){
				temp = authCode.split(",");
				authCodes  = Arrays.asList(temp);
			}
			roleAuthService.relate(new RoleAuthRelate(roleCode, temp));
			
			jsonSuccess(null);
		}catch(RuntimeException t){
			jsonError(null);
			throw t;
		}
		return null;
	}
	
}
